#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <string>
#include <sstream>
#include <fstream>
#include <iterator>

#include "rpsrandom.h"
#include "rpssequence.h"
#include "rpstraincvc.h"


////////////////////////////////////////////
//Public things
////////////////////////////////////////////
double movecount=1;

std::string showrps(int &move) { //saves some lines
	if (move==0) {
		return "R";
	} else if (move==1) {
		return "P";
	} else if (move==2) {
		return "S";
	}
}

int algomove(int algo, int prevmove) { //saves space, maybe. 
	if (algo==1) {
		return randomRPS();
	} else if (algo==2) {
		return sequenceRPS(prevmove);
	} else if (algo==3) { //still working on it
		/*int cutoff=5; //modify cutoff to change length(s) of substrings for pattern matching
			
		if(movecount<cutoff){
						
			moveseq.append(showrps(prevmove));

			//initialize history file
			std::ofstream datafile;
			datafile.open("cvcmovehistory.txt", std::ofstream::app);
			datafile << showrps(prevmove);
			datafile.close();
			// std::cout << moveseq << std::endl;
			cmove=randomRPS();
		}
		if(movecount>=cutoff){
						
			//////////////////////////////////////////////////////
			// Pattern Selection
			// Runs training function for subsequences of size
			// 1 up to the cutoff
			// Pattern with the highest confidence guess is selected
			//////////////////////////////////////////////////////
						
			double maxp=0;
			int bestmove;
			int pattern;
						
			std::vector< std::pair<int, double> > guessvector;
						
			for(int i=1; i<=cutoff-1; i++){
					std::pair<int, double> guess=trainRPS(moveseq.substr(cutoff-1-i, i), i);
					if(guess.second==1) guess.second=0; //ignore guesses based on only 1 observation
					guessvector.push_back(guess);
								
					if(guess.second>maxp){
						maxp=guess.second;
						bestmove=guess.first;
						pattern=i;
					}
			}	
						
			double pbound=0.5; //probability threshold--ensures algorithm is not too deterministic
						
			//less stringent threshold when player's reaction time is faster than average
			if(time < (0.75*rxnmean)){
				pbound=0.4;
			}
						
			//verify best guess meets probability threshold
			bool inbounds=false;
			for(int j=0; j<guessvector.size(); j++){
				if(inBounds(guessvector[j].second, pbound, 1.0)){
					inbounds=true;
					break;
				}
			}
						
			if(inbounds){
				cmove=bestmove;
				// std::cout << "Matching last " << pattern << " moves. Computer will play: " << showrps(bestmove) << ". Confidence: " << maxp << std::endl;
			} else {
				cmove=randomRPS();
				// std::cout << "Computer lacks confidence. Randomizing guess." << std::endl;
			}
						
			if(lossstreak>3){
				cmove=randomRPS();
				// std::cout << "Lost three in a row...changing strategy" << std::endl;
			}
						
						
			//////////////////////////////////////////////////////
			// After computer move determined, update 
			// and add player move to data file
			//////////////////////////////////////////////////////
			moveseq.append(showrps(prevmove));
			moveseq.erase(0,1);
						
			std::ofstream datafile;
			datafile.open("cvcmovehistory.txt", std::ofstream::app);
			datafile << showrps(prevmove);
			datafile.close();
		}*/
		return 2;
	}


}



int main(int argc, char** argv) {
	std::string input;
	std::vector<int> wtl (3,0); //win-tie-loss record. Don't play more than 2^31 times
	int cmove1; //computer 1's. Rock is 0, Paper is 1, Scissors is 2.
	int cmove2; //computer 2's move.
	int prevmove1=0;
	int prevmove2=0;

	std::cout << "Choose Algorithm 1: 1 for random, 2 for sequence, 3 for training" << std::endl;
	int algo1;
	std::cin >> algo1;
	std::cout << std::endl;
	std::cout << "Choose Algorithm 2: 1 for random, 2 for sequence, 3 for training" << std::endl;
	int algo2;
	std::cin >> algo2;
	std::cout << std::endl;

	std::cout << "How many rounds?" << std::endl;
	double rounds;
	std::cin >> rounds;
	std::cout << std::endl;
	

	std::string moveseq;
	while (movecount<=rounds) {
		
		cmove1=algomove(algo1,prevmove2);
		cmove2=algomove(algo2,prevmove1);
		prevmove1=cmove1;
		prevmove2=cmove2;

		if (cmove1==cmove2) {
			wtl[1]++;
		} else if (cmove2==(cmove1+1)%3) {
			wtl[2]++;
		} else {
			wtl[0]++;
		}

		

		movecount++;
	}

	std::cout << "Final W1-T-W2 record: " << wtl[0] << "-" << wtl[1] << "-" << wtl[2] << std::endl;
	return 0;
}
